<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>渠道登录</title>
    <link rel="stylesheet" type="text/css" href="css/normalize.css" />
    <link rel="stylesheet" type="text/css" href="css/login.css" />
    <link rel="stylesheet" href="css/supersized.css">
    <link rel="shortcut icon" href="img/yjlogo.ico" type="image/x-icon" />
    <script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
    <script src="js/supersized.3.2.7.min.js"></script>
    <script src="js/supersized-init.js"></script>
</head>

<body>
    <div id="channel-login">
        <div class="channel-login-top"></div>
        <div class="channel-login-user">
            <p class="login-user-header">友荐金融安全管理系统</p>
            <div class="login-use-content">
                <p class="login-username">
                    <input type="text" placeholder="用户名" /><span></span></p>
                <p class="login-password">
                    <input type="password" placeholder="密码" /><span></span></p>
                <p class="login-btn">
                    <input type="button" value="登 录" />
                </p>
            </div>
        </div>
        <div class="login_swiper"></div>
    </div>
    <script src="js/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/jquery.cookie.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
    // var host = "http://106.14.57.180:8001";
    // var host = "http://192.168.6.223:1234";
    var host = "http://114.215.252.114:8000";
    var usernameError = false;
    var passwordError = false;
    $('.login-btn input').on('click', function() {
        var username = $('.login-username input').val();
        var passwords = $('.login-password input').val();
        if (username == '') {
            $('.login-username input').css({
                "border": "1px solid white",
                "color": "#c0392b"
            }).attr("placeholder", "请输入用户名");
            usernameError = true;
        };
        if (passwords == '') {
            $(".login-password input").css({
                'border': '1px solid white',
                "color": "#c0392b"
            }).attr("placeholder", "请输入密码");
            passwordError = true;
        }

        var data = {
            'username': username,
            'password': passwords,
        }
        $.ajax({
            type: "POST",
            url: host + "/channel/login/",
            datatpye: 'json',
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            success: function(res) {
                console.log(res);
                $.cookie('channeltoken', res.token, {
                    expires: 7
                });
                $.cookie('permisssion', res.permission, {
                    expires: 7
                });
                // localStorage.setItem("permission": res.permission);
                if (res.status == 'success') {
                    window.location.href = 'channel.html';
                }
            },
            error: function(res) {
                console.log(res);
                var str = res.responseText;
                var obj2 = eval("(" + str + ")");
                if (obj2.message) {
                    alert(obj2.message)
                } else {
                    alert(obj2.detail)
                }
            }　　

        });
    })
    $(document).on('keydown', function(event) {
        if (event.keyCode == 13) {
            var username = $('.login-username input').val();
            var passwords = $('.login-password input').val();

            if (username == '') {
                $('.login-username input').css({
                    "border": "1px solid white",
                    "color": "#c0392b"
                }).attr("placeholder", "请输入用户名");
                usernameError = true;
            };
            if (passwords == '') {
                $(".login-password input").css({
                    'border': '1px solid white',
                    "color": "#c0392b"
                }).attr("placeholder", "请输入密码");
                passwordError = true;
            }

            var data = {
                'username': username,
                'password': passwords,
            }

            $.ajax({
                type: "POST",
                url: host + "/channel/login/",
                datatpye: 'json',
                data: JSON.stringify(data),
                contentType: "application/json; charset=utf-8",
                success: function(res) {
                    console.log(res);
                    $.cookie('channeltoken', res.token, {
                        expires: 7
                    });
                    $.cookie('permisssion', res.permission, {
                        expires: 7
                    });

                    if (res.status == 'success') {
                        window.location.href = 'channel.html';
                    }

                },
                error: function(res) {
                    console.log(res);
                    var str = res.responseText;
                    var obj2 = eval("(" + str + ")");
                    if (obj2.message) {
                        alert(obj2.message)
                    } else {
                        alert(obj2.detail)
                    }
                }
            });
        }
    })

    $(".login-username input").on("click", function() {
        if (usernameError == true) {
            $(this).css({
                "border": "1px solid #f39c12",
                "color": "#34495e"
            }).attr("placeholder", "用户名").val("");
        };
        usernameError = false;
    });

    $(".login-password input").on("click, focus", function() {
        if (passwordError == true) {
            $(this).css({
                "border": "1px solid #f39c12",
                "color": "#34495e"
            }).attr({
                "placeholder": "密码"
            }).val("");
        };
        passwordError = false;
    })
    </script>
</body>

</html>
